﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RollMove : MonoBehaviour {

    private Transform tr;
    public Transform camera;
    private CharacterController controller;
    //键盘输入值变化、移动速度、旋转速度
    private float h = 0.0f;
    private float v = 0.0f;
    private float movSpeed = 20.0f;
    private float rotSpeed = 1000.0f;
    //移动方向向量变量
    private Vector3 movDir = Vector3.zero;
    float timelost = 0.0f; //记录时间
    void Start()
    {
        tr = GetComponent<Transform>();
        controller = GetComponent<CharacterController>();

    }


    void Update()
    {
        //摄像机朝向
        Vector3 f = transform.forward;
        Vector3 forward = camera.transform.forward;
        Vector3 right = camera.transform.right;

        h = Input.GetAxis("Horizontal");
        v = Input.GetAxis("Vertical");
        //不在Y方向旋转
        f.y = 0;
        forward.y = 0;
        right.y = 0;
        //变为单位向量
        f.Normalize();
        forward.Normalize();
        right.Normalize();
        transform.forward = right * h + forward * v;
        if (Mathf.Abs(h) > 0 || Mathf.Abs(v) > 0)
        {
            controller.Move(transform.forward * Time.deltaTime * movSpeed);
        }
        
    }
}
